package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = stringRedisTemplate.keys(pattern);
        stringRedisTemplate.delete(keys);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public Result update(SetmealDTO setmealDTO) {
        //1.非空判断
        if (setmealDTO.getId() == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.拷贝属性 setmealDTO 到 setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //3.调用Mapper层修改套餐
        setmealMapper.update(setmeal);

        //4.删除套餐相关联的菜品
        setmealDishMapper.delete(Arrays.asList(setmealDTO.getId()));

        //5.获取修改套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            //6.批量添加菜品
            setmealDishMapper.batchAdd(setmealDishes);
        }
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("Setmeal*");
            return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id) {
        //1.非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.调用Mapper层根据id查询
        Setmeal setmeal = setmealMapper.getById(id);

        //3.查询相关联的菜品
       List<SetmealDish> dish= setmealDishMapper.getDishBySetmealId(id);
        //4.封装到vo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dish);

        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Result delete(List<Long> ids) {
        //1.非空判断
        if (ids == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.判断是否起售，若是，则不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                return Result.error(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //3.调用Mapper层根据id删除套餐
        setmealMapper.delete(ids);

        //4.删除套餐关联的菜品
        setmealDishMapper.delete(ids);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("Setmeal*");
        return Result.success();
    }

    /**
     * 套餐起售或停售
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result startOrClose(Integer status, Long id) {
        //1.非空判断
        if (status == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //2.赋值给套餐
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //3.调用Mapper层修改套餐
        setmealMapper.update(setmeal);

        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("Setmeal*");
        return Result.success();
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    @Override
    public Result add(SetmealDTO setmealDTO) {
        //1.非空判断
        if (setmealDTO.getName() == null) {
            return Result.error(MessageConstant.SETMEAL_NOT_FOUND);
        }

        //2.拷贝属性，setmealDTO 到setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //3.调用Mapper层添加套餐
        setmealMapper.add(setmeal);

        //4.循环获取菜品关联的套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            //5.调用Mapper层添加菜品
            setmealDishMapper.batchAdd(setmealDishes);
        }
        //清理缓存数据
        String key = "Setmeal_" + setmealDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.非空判断,设置默认
        if (setmealPageQueryDTO.getPage() == null) {
            setmealPageQueryDTO.setPage(0);
        }
        if (setmealPageQueryDTO.getPageSize() == null) {
            setmealPageQueryDTO.setPageSize(10);
        }
        //2.开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //3.调用Mapper分页查询
        Page<SetmealVO> setmealVO = setmealMapper.page(setmealPageQueryDTO);

        //4.封装到pageResult
        PageResult pageResult = new PageResult(setmealVO.getTotal(), setmealVO.getResult());

        return Result.success(pageResult);
    }
}
